<?php
/**
 * Template Name: Manage Course
 * Description: A Page Template that showcases Sticky Posts, Asides, and Blog Posts
 *
 * The showcase template in Twenty Eleven consists of a featured posts section using sticky posts,
 * another recent posts area (with the latest post shown in full and the rest as a list)
 * and a left sidebar holding aside posts.
 *
 * We are creating two queries to fetch the proper posts and a custom widget for the sidebar.
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */
get_header() ?>

<?php   
    require('./wp-blog-header.php');
    require('DBHandler.php');

    $dbHandler = new DBHandler;

    $heading = "Add a new course";
    $id = '';
    
    if ($_GET)
    {
        if ($_GET['id'])
        {
            $id = $_GET['id'];
            $op = $_GET['op'];

            if ($op == 'edit')
            {
                $heading = "Edit an existing course";
                $course = $dbHandler->GetCourse($id);
            }
            else if ($op == 'delete')
            {
                $dbHandler->DeleteCourse($id);
                header( 'http://students.washington.edu/andreybr/wordpress/?page_id=235' );
            }
        }
    }
    if ($_POST) 
    {
		echo "aaaaaa";
        if ($_POST['id'])
        {
            $dbHandler->UpdateCourse($_POST['id'], $_POST['txtName'], $_POST['selQuarter'], $_POST['txtYear'], $_POST['selFaculty']);
            header( 'http://students.washington.edu/andreybr/wordpress/?page_id=235' );
        }
        else
        {
            $dbHandler->AddCourse($_POST['txtName'], $_POST['selQuarter'], $_POST['txtYear'], $_POST['selFaculty']);
        }
    }
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Manage Course</title>
    </head>
    <body>
        <h1>
            <?php
                echo $heading;
            ?>
        </h1>
        <a href="http://students.washington.edu/andreybr/wordpress/?page_id=235">Back to Manage Courses</a>
        <br/><br/>
        <form action="http://students.washington.edu/andreybr/wordpress/?page_id=241" method="post">
            <?php
                echo "<input type=hidden name='id' value='" . $id . "' />";
            ?>
            <table>
                <tr>
                    <td>Course Name</td>
                    <td>
                        <input name="txtName" maxlength="40"
                           <?php
                                if ($course)
                                {
                                    echo "value='" . $course->Name . "'";
                                }
                           ?> />
                    </td>
                </tr>
                <tr>
                    <td>Course Quarter</td>
                    <td>
                        <select name="selQuarter">
                            <option value="Autumn" <?php if ($course && $course->Quarter == 'Autumn') echo "selected"?>>Autumn</option>
                            <option value="Spring" <?php if ($course && $course->Quarter == 'Spring') echo "selected"?>>Spring</option>
                            <option value="Summer" <?php if ($course && $course->Quarter == 'Summer') echo "selected"?>>Summer</option>
                            <option value="Winter" <?php if ($course && $course->Quarter == 'Winter') echo "selected"?>>Winter</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Year of Course</td>
                    <td>
                        <input name="txtYear" maxlength="4" 
                        <?php
                                if ($course)
                                {
                                    echo "value='" . $course->Year . "'";
                                }
                           ?> />
                    </td>
                </tr>
                <tr>
                    <td>Faculty</td>
                    <td>
                        <select name="selFaculty">
                            <?php
                                $rows = $dbHandler->GetAllFaculty();
                                
                                foreach($rows as $row)
                                {
                                    $output = "<option value='" . $row->Id . "' ";
                                    
                                    if ($course && $course->FacultyId == $row->Id)
                                    {
                                        $output .= " selected ";
                                    }

                                    $output .=  ">" . $row->Name . "</option>";
                                    
                                    echo $output;
                                }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" value="Submit"/></td>
                    <td><input type="reset" value="Reset"/></td>
                </tr>
            </table>
        </form>
    </body>
</html>


<?php get_footer() ?>